package cn.btks.resultintegration.catalog.controller;

import cn.btks.commonserver.result.Constants;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.util.GeoUtils;
import cn.btks.resultintegration.catalog.dto.CatalogDto;
import cn.btks.resultintegration.catalog.pojo.Catalog;
import cn.btks.resultintegration.catalog.service.CatalogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lijun
 * @Date 2020/12/14 17:32
 */
@RestController
@RequestMapping("api/v1.0/resultIntegration/catalog")
@Api(tags = "目录结构模块")
@Validated
public class CatalogController {

    @Autowired
    CatalogService catalogService;

    @GetMapping("/findAll")
    @ApiOperation("查询所有目录信息(树结构)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAll() {
        List<CatalogDto> catalogList = catalogService.findAll();
        return catalogList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), catalogList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findAll1")
    @ApiOperation("查询所有目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAll1() {
        List<Catalog> catalogList = catalogService.findAll1();
        return catalogList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), catalogList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id") Long id) {
        Catalog catalog = catalogService.findById(id);
        return catalog != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), catalog) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findByCode/{code}")
    @ApiOperation("根据code查询目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "code", value = "目录编码", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findByCode(@PathVariable("code") String code) {
        Catalog catalog = catalogService.findByCode(code);
        return catalog != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), catalog) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @GetMapping("/findByFilePath")
    @ApiOperation("根据code查询目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findByCode1(String filePath) {
        JSONArray shpFileAttributes = GeoUtils.getShpFileAttributes(filePath);
        for(int i = 0; i< shpFileAttributes.size(); i++){
            Object object = shpFileAttributes.get(i);
            JSONObject object1 = (JSONObject) object;
            if(object1.containsKey(Constants.HJYSGKFQBM)){
                System.out.println(object1.get(Constants.HJYSGKFQBM));
            }
            String s = object1.toJSONString();
            System.out.println(s);
        }
        String jsonString = shpFileAttributes.toJSONString();
        return new ResultState(StateInfo.FIND_OK.getCode(),StateInfo.FIND_OK.getMsg());
    }

    @GetMapping("/findAllByUpLevelId/{upLevelId}")
    @ApiOperation("根据上级目录ID查询目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "upLevelId", value = "上级目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByUpLevelId(@PathVariable("upLevelId") Long upLevelId) {
        List<Catalog> catalogList = catalogService.findAllByUpLevelId(upLevelId);
        return catalogList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), catalogList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增目录信息", notes = "新增目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "目录编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogName", value = "目录名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "level", value = "目录等级", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "upLevelId", value = "上级目录ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序", required = false, dataType = "int"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(@RequestBody Catalog catalog) {
        return catalogService.save(catalog);
    }

    @PostMapping("/saveAll")
    @ApiOperation(value = "批量新增目录信息", notes = "批量新增目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "目录编码", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogName", value = "目录名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "level", value = "目录等级", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "upLevelId", value = "上级目录ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序", required = false, dataType = "int"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState saveAll(@RequestBody List<Catalog> catalogList) {
        return catalogService.saveAll(catalogList);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新目录信息", notes = "更新目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "目录编码", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogName", value = "目录名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "level", value = "目录等级", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "upLevelId", value = "上级目录ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序", required = true, dataType = "int"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState update(@RequestBody Catalog catalog) {
        return catalogService.update(catalog);
    }

    @PutMapping("/updateAll")
    @ApiOperation(value = "批量更新目录信息", notes = "批量更新目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "目录编码", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "catalogName", value = "目录名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "level", value = "目录等级", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "upLevelId", value = "上级目录ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "order", value = "排序", required = true, dataType = "int"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState updateAll(@RequestBody List<Catalog> catalogList) {
        return catalogService.updateAll(catalogList);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除目录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "目录ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState delete(@PathVariable("id") Long id) {
        return catalogService.delete(id);
    }

}
